package com.ruoyi.task.springsched2;

import org.junit.Test;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.FixedDelayTask;
import org.springframework.scheduling.config.FixedRateTask;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Author alison
 * @Date 2024/12/1 20:51
 * @Version 1.0
 * @Description
 */
public class DirectSched {

    static ThreadPoolTaskScheduler taskScheduler;

    static {
        taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(8);
        taskScheduler.initialize();
    }

    @Test
    public void testCron() throws Exception {
//        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
//        taskScheduler.setPoolSize(8);
//        taskScheduler.initialize();
        String cronExpr = "0/2 * * * * ?";
        CronTask cronTask = new CronTask(() -> {
            System.out.println("cron task");
        }, new CronTrigger(cronExpr));
        taskScheduler.schedule(cronTask.getRunnable(), cronTask.getTrigger());

    }

    @Test
    public void testDelay() throws Exception {
        FixedDelayTask fixedDelayTask = new FixedDelayTask(() -> {
            System.out.println("fixed delay task");
        }, 1000, 1000);

        Date start = new Date(System.currentTimeMillis() + fixedDelayTask.getInitialDelay());
        taskScheduler.scheduleWithFixedDelay(fixedDelayTask.getRunnable(), start, fixedDelayTask.getInterval());
    }

    @Test
    public void testFixedRateTask() throws Exception {
        FixedRateTask fixedRateTask = new FixedRateTask(() -> {
            System.out.println("fixed rate task");
        }, 1000, 0);
        Date start = new Date(System.currentTimeMillis() + fixedRateTask.getInitialDelay());
        taskScheduler.scheduleAtFixedRate(fixedRateTask.getRunnable(), fixedRateTask.getInterval());
    }


    @Test
    public void testFixedRate() throws Exception {
        taskScheduler.schedule(() -> {
            System.out.println("schedule");
        }, new Date());
        taskScheduler.scheduleAtFixedRate(() -> {
            System.out.println("fixed rate");
        }, 1000);
        taskScheduler.scheduleWithFixedDelay(() -> {
            System.out.println("fixed delay");
        }, 1000);
    }

    @Test
    public void testCronValid() throws Exception {
        String cronExpr = "* * * * * ?";
        boolean validExpression = CronExpression.isValidExpression(cronExpr);
        System.out.println("validExpression = " + validExpression);
    }

    @Test
    public void testScheduledExecutorService() throws Exception {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.schedule(() -> {
            System.out.println("1");
        }, 1000, TimeUnit.SECONDS);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("1");
        }, 0, 1000, TimeUnit.SECONDS);
        scheduledExecutorService.scheduleWithFixedDelay(()->{
            System.out.println("1");
        }, 0, 1000, TimeUnit.SECONDS);
    }

}
